from myelin.server.nodetype.Transform import Transform
from nodetypes import config


class Transform(Transform):
    
    defaults = config.DEFAULTS
    
    def setup(self, context):
        request = context.request
        
        defaultMode = getattr(
            context.defaults,
            'default_mode'
        ) or self.defaults['mode']
        
        urlFormatter = context.urlFormatter
        urlFormatter.defaultParams['mode'] = defaultMode
        
        errors = {}
        mode = self.requestParamValidators.SingleString(
            'mode',
            request.args,
            errors
        )
        
        if not errors and mode in request.graphs.keys():
            context.mode = mode
            urlFormatter.urlParams['mode'] = mode
        else:
            context.mode = defaultMode
            
    def process(self, context, source):
        self.setup(context)
        
        context.model = context.nodeType.retrieveModel(context)
        
        doc, imt = self.newDocument().createDocument(context, source)
        
        return doc, imt, self.chain or context.chain
            
            
# cleanup
del config